package com.gdit.zax.impl;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gdit.zax.dao.PlanDao;
import com.gdit.zax.entitys.Plan;
import com.gdit.zax.entitys.TaskAll;
import com.gdit.zax.utils.DBUtil;
import com.gdit.zax.utils.LogUtil;
import com.gdit.zax.utils.SqlUtil;

public class PlanDaoImpl implements PlanDao {
	private PreparedStatement ps;
	private ResultSet rs;
	
	private Connection conn;
	private HttpServletRequest request;
	private HttpServletResponse response;
	
	
	public PlanDaoImpl(Connection conn,HttpServletRequest request, HttpServletResponse response) {
		this.conn = conn;
		this.request = request;
		this.response = response;
	}
	

	public boolean addPlan(Plan data) {
		try {
			String sql = "insert t_plan set UserId=?,Name=?,Type=?,PlanTime=?,Content=?,CourseId=?,Warn=?,OverSeer=?";
			ps = conn.prepareStatement(sql);
			ps.setInt(1, data.getUserid());
			ps.setString(2, data.getName());
			ps.setInt(3, data.getType());
			ps.setTimestamp(4, new Timestamp(data.getPlantime()));
			ps.setString(5, data.getContent());
			ps.setInt(6, data.getCouresid());
			ps.setInt(7, data.getWarn());
			ps.setString(8, data.getOverseer());
			
			int result = ps.executeUpdate();
			if (result!=0) {
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public boolean delPlan(int id) {
		String sql = "delete from t_plan where id=?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, id);
			
			int result = ps.executeUpdate();
			if (result!=0) {
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}


	public ArrayList<Plan> listPlan(int page, int onepagecount) {
		ArrayList<Plan> list = new ArrayList<Plan>();
		try {
			String sql = "select * from t_plan group by Id desc limit ?,?";
			ps = conn.prepareStatement(sql);
			ps.setInt(1, (page-1)*onepagecount);
			ps.setInt(2, onepagecount);
			
			rs = ps.executeQuery();
			while(rs.next()){
				list.add(DBUtil.getobject(rs, Plan.class));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}


	public ArrayList<Plan> listPlan(int uid, int page, int onepagecount) {
		ArrayList<Plan> list = new ArrayList<Plan>();
		try {
			String sql = "select * from t_plan where UserId=? group by Id desc limit ?,?";
			ps = conn.prepareStatement(sql);
			ps.setInt(1, uid);
			ps.setInt(2, (page-1)*onepagecount);
			ps.setInt(3, onepagecount);
			
			rs = ps.executeQuery();
			while(rs.next()){
				list.add(DBUtil.getobject(rs, Plan.class));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}


	public ArrayList<TaskAll> listTask(int uid, int page, int onepagecount) {
		String sql = SqlUtil.getSql("getUserTodayTask.sql");
		ArrayList<TaskAll> list = new ArrayList<TaskAll>();
		
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, uid);
			ps.setInt(2, uid);
			ps.setInt(3, uid);
			ps.setInt(4, (page-1)*onepagecount);
			ps.setInt(5, onepagecount);
			
			rs = ps.executeQuery();
			while(rs.next()){
				list.add(DBUtil.getJoinObject(rs, TaskAll.class));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}


	public Plan getPlanInfo(int pid) {
		String sql = "select * from t_plan where Id=?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, pid);
			
			rs = ps.executeQuery();
			if (rs.next()) {
				return DBUtil.getobject(rs, Plan.class);				
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}


	public Plan getMyLastPlanInfo(int uid) {
		String sql = "select * from t_plan WHERE t_plan.UserId=? group by Id desc limit 0,1";
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, uid);
			
			rs = ps.executeQuery();
			if (rs.next()) {
				return DBUtil.getobject(rs, Plan.class);				
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}
